﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using Entity;

namespace ef_CodeFirst.Helpers
{
    /// <summary>
    /// 前台View视图中对HtmlHelper的扩展
    /// </summary>
    public static class ViewExtensions
    {
        /// <summary>
        /// 生成分类下拉列表框,选中指定的项
        /// </summary>
        /// <param name="html"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static MvcHtmlString SelectList_Category(this HtmlHelper html, long selectedValue)
        {
            ef.ServiceInterface.ICategoryService iCategoryService = null;
            iCategoryService = new ef.ServiceImplement.CategoryService();
             StringBuilder sb = new StringBuilder();
            sb.Append("<select name='Category' id='Category'>");
            foreach (var i in iCategoryService.GetCategory())
            {
                if (i.ID == selectedValue && selectedValue != 0)
                    sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", i.ID, i.Name);
                else
                    sb.AppendFormat("<option value='{0}'>{1}</option>", i.ID, i.Name);
            }
            sb.Append("</select>");
            return MvcHtmlString.Create(sb.ToString());
        }
        /// <summary>
        /// 生成分类下拉列表框
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString SelectList_Category(this HtmlHelper html)
        {
            return SelectList_Category(html, 0);
        }
    }
}